var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    page: 1,
    img_url: c.img_url,
  },
  onLoad: function (options) {
    this.getData();
  },

  getData:function(){
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;

    var data = {
      url: "/zfshop/goodsagent/goodslists",
      page: that.data.page,
      limit: 10,
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        if (data.length > 0) {
          that.setData({
            page: that.data.page + 1
          });
        }
        for (var index in data) {
          data[index].pic = c.img_url + data[index].pic;
        }
        if (that.data.page ==2){
          that.setData({
            goodsList: data,
          });
        }else{
          that.setData({
            goodsList: that.data.goodsList.concat(data),
          });
        }
        
      } else {

      }

    });
  },
  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    this.onLoad();
  },
  ///下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      goodsList: [],
      page: 1,
    });
    this.onLoad();
  },
  addAgent: function (e) {
    let goods_id = e.currentTarget.dataset.id;
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;

    var data = {
      url: "/zfshop/goodsagent/add",
      goods_id: goods_id,
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        utils.errMsg("代理成功");
        that.setData({
          page:1
        });
        setTimeout(function () {
          that.getData();
        }, 1000);
      } else {

      }
    });
  }
})